home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / intrlib1.zip / EMS.C < prev    next >
C/C++ Source or Header  |  1990-12-09  |  5KB  |  164 lines

  1. /******************************************************************************
  2. * EMS - expand memory support routines.                          *
  3. *                                          *
  4. *                    Written by Gershon Elber,  Dec. 1990  *
  5. *******************************************************************************
  6. * History:                                      *
  7. *  9 Dec 90 - Version 1.0 by Gershon Elber.                      *
  8. ******************************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <dos.h>
  13. #include "intr_loc.h"
  14. #include "intr_ems.h"
  15.  
  16. /* #define DEBUG_EMS */
  17.  
  18. #define EMS_DEVICE_NAME "EMMXXXX0"
  19.  
  20. #define EMS_INT_NUM    0x67
  21.  
  22. #define EMS_SEGMENT    0x41
  23. #define EMS_ALLOC    0x43
  24. #define EMS_MAP        0x44
  25. #define EMS_FREE    0x45
  26.  
  27. static unsigned int Segment = 0;
  28.  
  29. /******************************************************************************
  30. * Returns TRUE iff EMS has been detected.                      *
  31. ******************************************************************************/
  32. IntrBType EMSDetected(void)
  33. {
  34.     char *p = (char *) getvect(EMS_INT_NUM);
  35.  
  36.     if (p == NULL) return FALSE;
  37.  
  38.     p = MK_FP(FP_SEG(p), 10);              /* Get pointer to device name. */
  39.  
  40.     if (strncmp(p, EMS_DEVICE_NAME, 8) != 0) return FALSE;
  41.  
  42.     Segment = EMSSegment();
  43.  
  44.     return TRUE;
  45. }
  46.  
  47. /******************************************************************************
  48. * Returns the segment of the base EMS address in the system, 0 if error.      *
  49. ******************************************************************************/
  50. unsigned int EMSSegment(void)
  51. {
  52.     union REGS regs;
  53.  
  54.     regs.h.ah = EMS_SEGMENT;
  55.  
  56.     int86(EMS_INT_NUM, ®s, ®s);
  57.  
  58.     if (regs.h.ah != 0) return 0;
  59.  
  60.     return regs.x.bx;
  61. }
  62.  
  63. /******************************************************************************
  64. * EMS alloc routine. Returns EMS handle if succesfull, 0 if failed.          *
  65. ******************************************************************************/
  66. int EMSAlloc(int NumPages)
  67. {
  68.     union REGS regs;
  69.  
  70.     regs.x.bx = NumPages;
  71.     regs.h.ah = EMS_ALLOC;
  72.  
  73.     int86(EMS_INT_NUM, ®s, ®s);
  74.  
  75.     if (regs.h.ah != 0) return 0;
  76.  
  77.     return regs.x.dx;
  78. }
  79.  
  80. /******************************************************************************
  81. * EMS mapping routine. The specified page number in the given EMS handle is   *
  82. * mapped to the specified physical page.                      *
  83. * Returns a pointer to physical address.                      *
  84. ******************************************************************************/
  85. void *EMSMap(int EMSHandle, int LogicPageNum, int PhysicPageNum)
  86. {
  87.     union REGS regs;
  88.  
  89.     regs.x.bx = LogicPageNum;
  90.     regs.x.dx = EMSHandle;
  91.     regs.h.al = PhysicPageNum;
  92.     regs.h.ah = EMS_MAP;
  93.  
  94.     int86(EMS_INT_NUM, ®s, ®s);
  95.  
  96.     if (regs.h.ah != 0) return NULL;
  97.  
  98.     return MK_FP(Segment + 0x0400 * PhysicPageNum, 0x0000);
  99. }
  100.  
  101. /******************************************************************************
  102. * EMS free routine. Frees the EMS handle.                      *
  103. ******************************************************************************/
  104. void EMSFree(int EMSHandle)
  105. {
  106.     union REGS regs;
  107.  
  108.     regs.x.dx = EMSHandle;
  109.     regs.h.ah = EMS_FREE;
  110.  
  111.     int86(EMS_INT_NUM, ®s, ®s);
  112. }
  113.  
  114. #ifdef DEBUG_EMS
  115.  
  116. /******************************************************************************
  117. * Simple tests for the above routines.                          *
  118. ******************************************************************************/
  119. void main(void)
  120. {
  121.     int Handle1, Handle2;
  122.     void *p1, *p2;
  123.  
  124.     if (EMSDetected())
  125.     fprintf(stderr, "EMS driver detected.\n");
  126.     else {
  127.     fprintf(stderr, "EMS driver not found.\n");
  128.         exit(1);
  129.     }
  130.  
  131.     if ((Handle1 = EMSAlloc(1)) > 0)
  132.     fprintf(stderr, "EMS alloc one page - got handle \#%d.\n", Handle1);
  133.     else {
  134.     fprintf(stderr, "EMS alloc failed.\n");
  135.         exit(2);
  136.     }
  137.  
  138.     if ((p1 = EMSMap(Handle1, 0, 0)) != NULL)
  139.     fprintf(stderr, "EMS map one page - address %p.\n", p1);
  140.     else {
  141.     fprintf(stderr, "EMS map failed.\n");
  142.         exit(2);
  143.     }
  144.  
  145.     if ((Handle2 = EMSAlloc(5)) > 0)
  146.     fprintf(stderr, "EMS alloc five page - got handle \#%d.\n", Handle2);
  147.     else {
  148.     fprintf(stderr, "EMS alloc failed.\n");
  149.         exit(2);
  150.     }
  151.  
  152.     if ((p2 = EMSMap(Handle2, 0, 1)) != NULL)
  153.     fprintf(stderr, "EMS map one page - address %p.\n", p2);
  154.     else {
  155.     fprintf(stderr, "EMS map failed.\n");
  156.         exit(2);
  157.     }
  158.  
  159.     EMSFree(Handle1);
  160.     EMSFree(Handle2);
  161. }
  162.  
  163. #endif /* DEBUG_EMS */
  164.